package com.hfmall.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hfmall.entity.HfCategory;
import com.hfmall.entity.HfClass;
import com.hfmall.entity.HfValue;
import com.hfmall.entity.ResponseCode;
import com.hfmall.service.impl.HfCategoryServiceImpl;
import com.hfmall.service.impl.HfClassServiceImpl;
import com.hfmall.service.impl.HfValueServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 *
 * @author 橘山修仙人  导航查询
 * </p>
 * @author liclo
 * @since 2021-08-12
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("category")
public class HfCategoryController {
    @Autowired
    HfCategoryServiceImpl hfCategoryService;
    @Resource
    HfClassServiceImpl hfClassService;//规格
    @Resource
    HfValueServiceImpl hfValueService;//值

    @RequestMapping("list") //查询三级导航 从1级开始 1-2-3
    public ResponseCode getALlHfCategory() {
        List<HfCategory> hfCategoryList = hfCategoryService.getCategoryByLv(1);
        ResponseCode responseCode = new ResponseCode(hfCategoryList);
        return responseCode;
    }

    @RequestMapping("categoryHot") //热门导航
    public ResponseCode getHotHfCategory() {
        List<HfCategory> hotCategoryList = hfCategoryService.list();
        ResponseCode responseCode = new ResponseCode(hotCategoryList);
        return responseCode;
    }

    /**
     * 分页查询所有一级分类信息
     *
     * @author liclo
     * @date 2021/8/13
     */
    @RequestMapping("flevel")
    public ResponseCode getFLevel(Integer current) {
        return new ResponseCode(hfCategoryService.getFLevel(current));
    }

    /**
     * 分页查询所有次级分类信息
     *
     * @author liclo
     * @date 2021/8/13
     */
    @RequestMapping("slevel")
    public ResponseCode getNextLeve(Integer current, @Param("pid") Integer pid) {
        return new ResponseCode(hfCategoryService.getSLevel(current, pid));
    }

    /**
     * 分页查询所有上级分类信息
     *
     * @author liclo
     * @date 2021/8/13
     */
    @RequestMapping("llevel")
    public ResponseCode getLastLeve(Integer current,
                                    @Param("level") String level,
                                    @Param("pid") Integer pid) {
        return new ResponseCode(hfCategoryService.getLastLevel(current, level, pid));
    }

    /**
     * 按树形结构返回所有分类
     *
     * @author liclo
     * @date 2021/8/13
     */
    @RequestMapping("tree")
    public ResponseCode tree() {
        return new ResponseCode(hfCategoryService.tree());
    }

    @RequestMapping("add")
    public ResponseCode add(@RequestBody HfCategory category) {
        hfCategoryService.save(category);
        return ResponseCode.SUCCESS;
    }

    @RequestMapping("AllClass") //根据商品类别id->(goodsinfo_pid)获取商品类别的所有规格和值
    public  ResponseCode  getAllClassAndValue(int id){
        HfCategory hfCategory= hfCategoryService.getHfCategoryLv1(id);
        List<HfClass> classes=hfClassService.getClassByCategoryId(hfCategory.getId());//类别的规格
        classes.forEach(c->{
           List<HfValue> values= hfValueService.getValueByClassId(c.getId());
           c.setValues(values);
        });
        return new ResponseCode(classes);
    }


    /**
     * 按树形结构返回所有三级分类
     *
     * @author liclo
     * @date 2021/8/18
     */
    @RequestMapping("all")
    public ResponseCode all() {
        return new ResponseCode(hfCategoryService.getAll());
    }

    @GetMapping("upnav")
    public ResponseCode upnav(String nav, Integer id) {
        UpdateWrapper<HfCategory> wrapper = new UpdateWrapper<>();
        wrapper.set("isnav", nav).eq("id", id);
        hfCategoryService.update(wrapper);
        return ResponseCode.SUCCESS;
    }

    @GetMapping("uphot")
    public ResponseCode uphot(String hot, Integer id) {
        UpdateWrapper<HfCategory> wrapper = new UpdateWrapper<>();
        wrapper.set("ishot", hot).eq("id", id);
        hfCategoryService.update(wrapper);
        return ResponseCode.SUCCESS;
    }

    @GetMapping("get")
    public ResponseCode get(Integer id) {
        QueryWrapper<HfCategory> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return new ResponseCode(hfCategoryService.getOne(wrapper));
    }

    @PostMapping("update")
    public ResponseCode update(@RequestBody HfCategory hfCategory) {
        UpdateWrapper<HfCategory> wrapper = new UpdateWrapper<>();
        wrapper.set("category", hfCategory.getCategory())
                .set("Category_id", hfCategory.getCategoryId())
                .set("isnav", hfCategory.getIsnav())
                .set("ishot", hfCategory.getIshot())
                .set("level", hfCategory.getLevel())
                .eq("id", hfCategory.getId());
        hfCategoryService.update(wrapper);
        return ResponseCode.SUCCESS;
    }

    @GetMapping("fcat")
    public ResponseCode fcat() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("Category_id", 0);
        return new ResponseCode(hfCategoryService.list(queryWrapper));
    }

    /**
     * 按树形结构返回全部分类
     */
    @RequestMapping("allHfCategoryList") //分页查询所有goodsinfo
    public ResponseCode getAllCategoryList() {
        return new ResponseCode(hfCategoryService.getCategoryByLv(1));
    }

    /**
     * 全部规格
     * 全部值
     */
    @RequestMapping("allHfClassValueList") //分页查询所有goodsinfo
    public ResponseCode getClassValueList() {
        List<List<String>> classValueList = new ArrayList<>();
        hfClassService.getAllOnceClass().forEach(c -> {//全部规格
            List<String> vK = new ArrayList<>();
            vK.add(c);

            hfValueService.getAllOnceValueByClassName(c).forEach(v -> {//规格-全部值
                vK.add(v);
            });
            classValueList.add(vK);
        });
//        log.debug("规格:{}", classValueList);
        return new ResponseCode(classValueList);
    }


}
